package com.qianchang.microfinance.view.mine.loan;

import android.os.Bundle;
import android.webkit.WebView;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.HtmlUtil;
import com.dm.library.utils.StringUtils;
import com.dm.library.utils.ToastUtil;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.LoanAgreementBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;

import java.io.Serializable;

public class LoanAgreementActivity extends BaseActivity {
    private Serializable item;
    private WebView tvproductintroduction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loan_agreement);
        initView();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.Label_loan_agreement));
        tvproductintroduction = (WebView) findViewById(R.id.tv_product_introduction);
        getData();
    }

    private void getData() {
        String projectId = getBundle().getString(ConstantAppInfo.PROJECT_ID);
        String productType = getBundle().getString(ConstantAppInfo.PROJECT_TYPE,"0");//0：现金贷、1：消费贷、2：抵押贷、3：租房分期、4：学生贷、5：业主贷
        if (projectId == null) {
            ToastUtil.getInstant().show(this, getString(R.string.data_error));
            return;
        }
        show();
        HttpParams params = new HttpParams();
        params.put("projectId", projectId);
        params.put("productType", productType);
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.FIND_AGREEMENT, params, new RequestCallBack<LoanAgreementBean>() {
            // {"code":"000000","data":{"agreementContent":"现金贷协议"},"description":"成功"}
            @Override
            public void onSuccess(JsonResult<LoanAgreementBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    try {
                        LoanAgreementBean bean = response.getObject();
                        String strContent = bean.getAgreementContent();
                        if (!StringUtils.isEmptyOrNull(strContent)) {
                            tvproductintroduction.loadDataWithBaseURL(null, strContent, "text/html", "UTF-8", null);
                            HtmlUtil htmlUtil = new HtmlUtil();
                            htmlUtil.setBody(new StringBuilder().append(strContent));
                            tvproductintroduction.loadDataWithBaseURL(DMConstant.Config.BASE_URL,
                                    htmlUtil.createHtml(), "text/html", "utf-8", null);
                        } else {
                            ToastUtil.getInstant().show(LoanAgreementActivity.this, getString(R.string.data_error));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtil.getInstant().show(LoanAgreementActivity.this, response.getDescription());
                }

            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(LoanAgreementActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }
}
